package com.neusoft.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import javax.sql.DataSource;

/**
 * @Author: duan
 * @Date: Create in 14:40 2020/10/28
 */
@Configuration
/*
    PropertySource：只是读取配置文件
        String[] value();返回值是数组
            "值"：表示单个
            {"值1", "值2"}
 */
@PropertySource("classpath:jdbc.properties")
public class JdbcConfig {
    @Value("${jdbc.username}")
    private String username;
    @Value("${jdbc.password}")
    private String password;
    @Value("${jdbc.url}")
    private String url;
    @Value("${jdbc.driver}")
    private String driver;

    /**
     * <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
     * <property name="username" value="${jdbc.username}"></property>
     * <property name="password" value="${jdbc.password}"></property>
     * <property name="driverClassName" value="${jdbc.driver}"/>
     * <property name="url" value="${jdbc.url}"/>
     * </bean>
     */
//    @Bean//注册bean，id为方法名称，class为方法的返回值类型
    @Bean("dataSource")
    public DataSource dataSource() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        dataSource.setDriverClassName(driver);

        return dataSource;
    }
}
